<?php

namespace Summer\Box\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use RainLab\User\Models\User as UserModel;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Box\Models\BoxOrderModel;
use Summer\Box\Models\Settings as BoxSettings;

use Summer\Hjd\Models\DetailedBonusModel;
use Summer\Hjd\Models\DetailedLineModel;
use Summer\Hjd\Models\DetailedScoreModel;
use Summer\Hjd\Models\DetailedStockModel;
use Summer\Hjd\Models\WalletModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Carbon\Carbon;

class IndexController extends Controller
{
    use ApiReturn;

    /**
     * 初始化
     */
    public function init(Request $request)
    {
        $box_settings= BoxSettings::instance();
        $sessions=$box_settings->sessions;
        foreach ($sessions as $k=>$v){
            $sessions[$k]['cover']=url('storage/app/media'.$v['cover']);
        }
        $return['sessions']=$sessions;
        return $this->apiSuccess('操作成功',$return);
    }

    /**
     * 收益数据显示
     */
    public function merchant(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $wallets=WalletModel::select('id','currency_slug','user_id','balance')
            ->where('user_id',$user->id)
            ->with(['currency'=>function($qc){
                $qc->select('id','slug','title');
            }])
            ->whereHas('currency',function ($qhc){
                $qhc->where('status',1);
            })
            ->get();
//        $tj_user=UserModel::where('id',$user->tuid)->first();

        //推荐的ids
        $tjdids=[];
        $tjdids=UserModel::where('tuid',$user->id)->pluck('id');

        //今日收益
        //商品预收益
        //$jrsy_pre_money=BoxOrderModel::where('buy_uid',$user->id)->whereDate('created_at',Carbon::today())->whereIn('status_slug',['shangjia_ing','finish','split'])->sum('pre_money');
        //增加了商品收益的币种，按币种来
        $jrsy_pre_money=DetailedLineModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_line')->whereDate('created_at',Carbon::today())->sum('money');

        $jrsy['pre_money']=$jrsy_pre_money??0;
        //直推收益
        $jrsy_zhitui=DetailedBonusModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_zhitui')->whereDate('created_at',Carbon::today())->sum('money');
        $jrsy['zhitui']=$jrsy_zhitui??0;
        //团队收益
        $jrsy_tuandui=DetailedStockModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_jicha')->whereDate('created_at',Carbon::today())->sum('money');
        $jrsy['tuandui']=$jrsy_tuandui??0;
        //今日业绩

        //直推的人的团队业绩
        $yeji_td_zs=UserModel::where('tuid',$user->id)->sum('yeji_self');
        $yeji_td_total_zs=UserModel::where('tuid',$user->id)->sum('yeji_self_total');
        $yeji_num=BoxOrderModel::whereIn('buy_uid',$tjdids)->whereDate('created_at',Carbon::today())->where('status','>',2)->count();
        $yeji_num_total=BoxOrderModel::whereIn('buy_uid',$tjdids)->where('status','>',2)->count();


        $jryj['yeji_td']=$user->yeji_td;
        $jryj['yeji_td_zs']=$yeji_td_zs??0;
        $jryj['yeji_num']=$yeji_num;
        //累计业绩
        $ljsy['yeji_td_total']=$user->yeji_td_total;
        $ljsy['yeji_td_total_zs']=$yeji_td_total_zs??0;
        $ljsy['yeji_num_total']=$yeji_num_total;

        //团队人数
        $tuandui=[];
        $tuandui['active']=UserModel::whereRaw("FIND_IN_SET(?,`tids`)",[$user->id])->where('yeji_self','>',0)->count();
        $tuandui['unactive']=UserModel::whereRaw("FIND_IN_SET(?,`tids`)",[$user->id])->where('yeji_self',0)->count();
        $return['tuandui']=$tuandui;
        $return['wallets']=$wallets;
        $return['jrsy']=$jrsy;//今日收益
        $return['jryj']=$jryj;//今日业绩
        $return['ljsy']=$ljsy;//累计业绩

        //for-jikj1008
//        $jrsy_pre_money_top=BoxOrderModel::where('buy_uid',$user->id)->whereIn('status_slug',['shangjia_ing','finish','split'])->sum('pre_money');
        $jrsy_pre_money_top=DetailedLineModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_line')->sum('money');

        $ljsy_bonus=DetailedBonusModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_zhitui')->sum('money');
        $ljsy_score=DetailedScoreModel::where('user_id',$user->id)->where('inout',1)->where('change_slug','bonus_jiantui')->sum('money');
        $ljsy_stock=DetailedStockModel::where('user_id',$user->id)->where('inout',1)->whereIn('change_slug',['bonus_jicha','bonus_fenhong'])->sum('money');
        $ljsy_top=$ljsy_bonus+$ljsy_score+$ljsy_stock+$jrsy_pre_money_top;
        $ljsy_top = sprintf("%.2f",$ljsy_top);
        $return['ljsy_top']=$ljsy_top;
        return $this->apiSuccess('操作成功',$return);

    }



}
